package com.gy.api.pc.content.service.impl;

import com.gy.api.base.Type;
import com.gy.api.pc.content.dao.BannerDao;
import com.gy.api.pc.content.domain.po.BannerPO;
import com.gy.api.pc.content.domain.po.PosterPO;
import com.gy.api.pc.content.domain.vo.*;
import com.gy.api.pc.content.service.IBannerService;
import com.gy.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
@Slf4j
public class BannerServiceImpl implements IBannerService {
    @Autowired
    BannerDao bannerDao;

    @Override
    public List<BannerVO> selectBannerPageList(BannerSelectListPageDto bannerList) {
        return bannerDao.selectBannerPageList(bannerList);
    }

    @Override
    public List<BannerVO> selectBannerTypeList(BannerSelectListDto bannerList) {
        return bannerDao.selectBannerTypeList(bannerList);
    }

    @Override
    public void addSave(BannerAddVo addDto) {
        BannerPO bannerPO=new BannerPO();
        BeanUtils.copyProperties(addDto,bannerPO);
        bannerPO.preInsert();
        bannerDao.insertSelective(bannerPO);

    }

    @Override
    public void editSave(BannerUpdateVo updateDto) {
        BannerPO bannerPO=bannerDao.selectByPrimaryKey(updateDto.getId());
        if(bannerPO==null){
            throw new BusinessException(Type.FAIL.getCode(),"信息未找到！");
        }
        BeanUtils.copyProperties(updateDto,bannerPO);
        bannerPO.preUpdate();
        bannerDao.updateByPrimaryKeySelective(bannerPO);
    }

    @Override
    public void editState(BannerStateUpdateDto updateDto) {
        BannerPO bannerPO=bannerDao.selectByPrimaryKey(updateDto.getId());
        if(bannerPO==null){
            throw new BusinessException(Type.FAIL.getCode(),"信息未找到！");
        }
        bannerPO.setIsTopped(updateDto.getIsTopped());
        bannerPO.preUpdate();
        bannerDao.updateByPrimaryKeySelective(bannerPO);
    }

    @Override
    public void deleteBannerByBannerIds(List<String> ids) {
        for (String id:ids) {
            BannerPO bannerPO=bannerDao.selectByPrimaryKey(id);
            if(bannerPO==null){
                throw new BusinessException(Type.FAIL.getCode(),"信息未找到！");
            }
            bannerPO.preDelete();
            bannerDao.updateByPrimaryKeySelective(bannerPO);
        }
    }
}